﻿using UnityEngine;
using System.Collections;
using MoreMountains.Tools;

namespace MoreMountains.CorgiEngine
{
    /// Add this class to a Character and it'll be able to dive by pressing down + dash button, pounding the ground in the process.
    public class CharacterBeHurt : CharacterAbility
    {
        /// <summary> 硬直时间 </summary>  
        public float RigidityTime = 0.35f;

        protected override bool CheckHandleInput()
        {
            //  不检测按键  所以此函数为false
            return false;
        }

        /// <summary> 这是一个类似于update的时点   并且也是在update中，只是调用 不在这个类 </summary>  
        public override void ProcessAbility()
        {
            if (CheckBeHurt() == false)
                return;

            StartCoroutine(BeHurt());
        }
        public void EnterHurtState()
        {
            //_movement.ChangeState(CharacterStates.MovementStates.BeHurt);
            //_character.IntoBeHurt = true;
            StopAllCoroutines();
            StartCoroutine(BeHurt());
        }
        protected virtual bool CheckBeHurt()
        {
            if (AbilityPermitted // if the ability is permitted
                && (_movement.CurrentState == CharacterStates.MovementStates.BeHurt)
                && (_condition.CurrentState == CharacterStates.CharacterConditions.Normal) // or if we're under normal conditions
                && (_character.IntoBeHurt))
            {
                //  进入后  重置这个标志
                _character.IntoBeHurt = false;
                return true;
            }

            return false;
        }
        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.Q))
            {
                EnterHurtState();
            }
        }
        /// <summary>
        /// Coroutine used to make the player dive vertically
        /// </summary>
        protected virtual IEnumerator BeHurt()
        {
            Debug.Log("  进入  ");
            // we start our sounds
            PlayAbilityStartSfx();
            PlayAbilityUsedSfx();

            // we make sure collisions are on
            _controller.CollisionsOn();

            //  不需要转  因为只有这个状态才能进入到这里
            _movement.ChangeState(CharacterStates.MovementStates.BeHurt);

            //  禁用控制
            ControlInput(false);
            //  速度为 0
            _controller.SetForce(Vector2.zero);

            yield return new WaitForSeconds(RigidityTime);

            // while the player is not grounded, we force it to go down fast
            //while (!_controller.State.IsGrounded && _character.ConditionState.CurrentState != CharacterStates.CharacterConditions.Dead)
            //{
                
            //    yield return null; //go to next frame
            //}

            if (_character.ConditionState.CurrentState != CharacterStates.CharacterConditions.Dead)
            {
                // we play our exit sound
                StopAbilityUsedSfx();
                PlayAbilityStopSfx();

                //   restore the state to false
                _movement.ChangeState(CharacterStates.MovementStates.Idle);
            }

            //  恢复控制
            ControlInput(true);
        }

        /// <summary>
        /// Adds required animator parameters to the animator parameters list if they exist
        /// </summary>
        protected override void InitializeAnimatorParameters()
        {
            RegisterAnimatorParameter("BeHurt", AnimatorControllerParameterType.Bool);
        }

        /// <summary>
        /// Sends the current state of the Diving state to the character's animator
        /// </summary>
        public override void UpdateAnimator()
        {
            MMAnimator.UpdateAnimatorBool(_animator, "BeHurt", _movement.CurrentState == CharacterStates.MovementStates.BeHurt, _character._animatorParameters);
        }
    }
}
